from moodlefuse.files import Directory, RegularFile, FileState, FileSource
from moodlefuse.fuse import FuseOSError, EROFS
from stat import S_IFDIR, S_IFREG

class AssignmentFile(RegularFile):

    def __init__(self, api, name, url, **kwargs):
        super(AssignmentFile, self).__init__(api, name, url, **kwargs)
        
        self.assignment_url = kwargs['assignment_url']

    def sync(self):
        if self.state == FileState.DIRTY:
            self.api.submit_assignment_file(self)
            self.state = FileState.CLEAN
    
class AssignmentInformation(RegularFile):

    def __init__(self, api, name, url, **kwargs):
        super(AssignmentInformation, self).__init__(api, name, url, **kwargs)
        
        self.create_cache_file()
        #self.write("%s \n\nod %s do %s" % (instructions, start_date, end_date), 0)

class Assignment(Directory):

    def load_files(self):
        self.files = self.api.get_assignment_files(self.url)
        self.files['Navodila.txt'] = AssignmentInformation(self.api, 'Navodila.txt', self.url, source=FileSource.DOWNLOADED)
        
    def create_file(self, name, mode):
        file = AssignmentFile(self.api, name, None, assignment_url=self.url)
        file.attrs['st_nlink'] += 1
        file.create_cache_file()
        
        self.files[name] = file

class Assignments(Directory):

    def __init__(self, api, name, url, **kwargs):
        super(Assignments, self).__init__(api, name, url, **kwargs)

        self.attrs['st_mode'] = (S_IFDIR | 0400)

    def load_files(self):
        self.files = self.api.get_assignments(self.url)

    def create_file(self, name, mode):
        raise FuseOSError(EROFS)

  